﻿<div id="grid"></div>
<div id="dlgAdd" style="display:none;padding-left:60px;">
    <form id="formAdd"></form>
</div>
<div id="dlgUpdate" style="display:none;padding-left:60px;">
    <form id="formUpdate"></form>
</div>
<div id="dlgSetRole" style="display:none;">
    <div id="gridRoles"></div>
</div>

<script type="text/javascript">
    var _window = {};
    _window.g = {
        OK: '确定',
        CANCEL: '取消',
        NAME: 'Flow'
    };

    _window.click = function (item) {
        var m = _window[item.method];
        if (m) m();
    }

    _window.options = {
        /*grid*/
        title: '审批流信息',
        columns: [
            /* 表格字段 */
            { display: '', name: 'Id', align: 'left', hide: true }
            , { display: '审批流名称', name: 'FlowName', align: 'left', width: 150, minwidth: 120 }
            , { display: '表单名称', name: 'FormName', align: 'left', width: 150, minwidth: 120 }
            , { display: '流程简介', name: 'Description', align: 'left', width: 200, minwidth: 120 }
            , {
                display: '状态', name: 'State', align: 'left', width: 150, minwidth: 120,
                render: function (item) {
                    return item.State == 1 ? '未发布' : '已发布';
                }
            }
            , {
                display: '定义审批过程', align: 'left', width: 200, minwidth: 120,
                render: function (item) {
                    if (item.State == 1 || item.State == 2) {
                        return '<a href="/WF/Home/Nodes?id=' + item.Id + '" target="_self">管理</a>'
                    }
                    else {
                        return '';
                    }
                }
            }
        ],
        toolbar: [
            { method: 'add', text: '增加', click: _window.click, icon: 'add' }
            , { line: true }
            , { method: 'update', text: '修改', click: _window.click, icon: 'modify' }
            , { line: true }
            , { method: 'delete1', text: '删除', click: _window.click, icon: 'delete' }
            , { line: true }
            , { method: 'publish', text: '发布', click: _window.click, icon: 'modify' }
            , { line: true }
            , { method: 'setrole', text: '分配角色', click: _window.click, icon: 'role' }

        ],
        roleColumns: [
            { diaplay: '', name: 'id', hide: true }
            , { display: '', name: 'checked', hide: true }
            , { display: '角色名称', name: 'name', width: 350 }
        ],

        /*dialog*/
        titleAdd: '添加',
        titleUpdate: '修改',
        dlgWidth: 440,
        dlgHeight: 280,

        /*form*/
        addFields: [
            /* 表单字段 */
            {
                label: '审批流名称', name: 'FlowName', newline: true, type: 'text',
                validate: { required: true }
            }
            , {
                label: '表单', name: 'FormId', newline: true, type: 'select',
                comboboxName: 'cboForms',
                validate: { required: true },
                options: {
                    textField: 'text',
                    valueField: 'value'
                }
            }
            , { label: '流程简介', name: 'Description', newline: true, type: 'textarea' }
        ],
        updateFields: [
            /* 表单字段 */
            {
                label: '审批流名称', name: 'FlowName', newline: true, type: 'text',
                validate: { required: true }
            }
            , {
                label: '流程简介', name: 'Description', newline: true, type: 'textarea'
            }
            , { name: 'Id', type: 'hidden' }
            , { name: 'FormId', type: 'hidden' }
        ],
        inputWidth: 170,
        labelWidth: 90,
        space: 40,
        addPrefixID: 'formAdd_',
        updatePrefixID: 'formUpdate_',

        updateQuestion: '请先选择需要修改的行',
        deleteQuestion: '请先选择需要删除的行',
        deleteConfirm: '是否删除当前选择项？'
    };

    _window.control = {
        grid: null,
        dlgAdd: null,
        dlgUpdate: null,
        formAdd: null,
        formUpdate: null
    };

    _window.getAddControl = function (id) {
        return liger.get(this.options.addPrefixID + id);
    }

    _window.getUpdateControl = function (id) {
        return liger.get(this.options.updatePrefixID + id);
    }

    _window.load = function (g, url, d) {
        var complete = false;
        $.post(url, d, function (data) {
            complete = true;
            if (data.code) {
                $.ligerDialog.error('加载失败, 原因: ' + data.msg);
            }
            else {
                g.loadData(data);                
            }
            g.gridloading.hide(100);
        }, 'json');

        var n = setInterval(function () {
            clearInterval(n);
            if (!complete) {
                g.gridloading.show();
            }
        }, 100);
    }

    _window.init = function () {
        $.metadata.setType("attr", "validate");
        this.form();
        this.grid();
    }

    _window.form = function (name) {
        if (name) return _window.control[name];

        _window.control.formAdd = $('#formAdd').ligerForm({
            prefixID: _window.options.addPrefixID,
            inputWidth: _window.options.inputWidth,
            labelWidth: _window.options.labelWidth,
            space: _window.options.space,
            fields: _window.options.addFields,
            validate: {}
        });

        _window.control.formUpdate = $('#formUpdate').ligerForm({
            prefixID: _window.options.updatePrefixID,
            inputWidth: _window.options.inputWidth,
            labelWidth: _window.options.labelWidth,
            space: _window.options.space,
            fields: _window.options.updateFields,
            validate: {}
        });
    }

    _window.dialog = function (name, row) {
        var id = 'dlg' + name;
        if (this.control[id]) {
            this.control[id].show();
        }
        else {
            this.control[id] = $.ligerDialog.open({
                show: false,
                target: $('#' + id),
                width: this.options.dlgWidth,
                height: this.options.dlgHeight,
                title: this.options["title" + name],
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: _window.g.OK, onclick: function () {
                            var m = _window['On' + name];
                            if (m) m();
                        }
                    },
                    {
                        text: _window.g.CANCEL, onclick: function () {
                            _window.control[id].hide();
                        }
                    }
                ]
            });
        }

        _window.control[id].row = row || null;
        var event = _window['On' + name + 'Show'];
        if (event)
            event(row);
    }

    _window.grid = function () {
        if (this.control.grid)
            return this.control.grid;

        this.control.grid = $('#grid').ligerGrid({
            title: this.options.title,
            columns: this.options.columns,
            toolbar: { items: this.options.toolbar },
            rownumbers: true,
            resizable: false,
            frozen: false,
            pageSize: 20,
            dataAction: 'local'
        });
        
        return this.control.grid;
    }

    _window.add = function () {
        _window.dialog('Add');
    }

    _window.update = function () {
        DoAction(
            _window.control.grid,
            function (row) {
                _window.dialog('Update', row);
            },
            _window.options.updateQuestion);
    }

    _window.publish = function () {
        DoAction(_window.control.grid, function (row) {
            if (row.State != 1) {
                $.ligerDialog.warn('该流程已经发布', '提示');
            } else {
                $.post('/API/Flow/Publish', row, function (res) {
                    if (res.code == 0) {
                        $.ligerDialog.success('发布成功');
                        _window.load(_window.control.grid, '/API/Flow/Query', {});
                    } else {
                        $.ligerDialog.error('发布失败, 原因: ' + res.msg);
                    }
                }, 'json');
            }
        }, '请选择需要发布的流程');
    }

    _window.setrole = function () {
        DoAction(
            _window.control.grid,
            function (row) {
                _window.dialog('SetRole', row);
            },
            _window.options.updateQuestion);
    }

    /* TODO: 补充代码, this != _window */
    _window.OnAdd = function () {
        if (_window.control.formAdd.valid()) {
            $.post('/API/Flow/Create', _window.control.formAdd.getData(), function (data) {
                if (data.code == 0) {
                    $.ligerDialog.success('添加成功', '提示');
                    _window.load(_window.control.grid, '/API/Flow/Query', {});
                    _window.control.dlgAdd.hide();
                } else {
                    $.ligerDialog.error(data.msg, '错误');
                }
            }, 'json');
        } else {
            _window.control.formAdd.showInvalid()
        }
    }

    _window.OnAddShow = function () {
        var cboForms = _window.getAddControl('cboForms');
        cboForms.setUrl('/API/Form/GetForms');
        cboForms.reload();
    }

    _window.OnUpdate = function () {
        if (_window.control.formUpdate.valid()) {
            $.post('/API/Flow/Update', _window.control.formUpdate.getData(), function (data) {
                if (data.code == 0) {
                    $.ligerDialog.success('修改成功', '提示');
                    _window.load(_window.control.grid, '/API/Flow/Query', {});
                    _window.control.dlgUpdate.hide();
                } else {
                    $.ligerDialog.error(data.msg, '错误');
                }
            }, 'json');
        } else {
            _window.control.formUpdate.showInvalid()
        }
    }

    _window.OnUpdateShow = function (row) {
        _window.control.formUpdate.setData(row);
    }

    _window.OnDelete = function (row) {
        if (row.State != 1) {
            $.ligerDialog.warn('已发布流程请联系管理员删除');
            return;
        }

        $.post('/API/Flow/Delete', row, function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('删除成功', '提示');
                _window.load(_window.control.grid, '/API/Flow/Query', {});
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    _window.OnSetRole = function () {
        var row = _window.control.dlgSetRole.row;
        var roles = [];
        var data = _window.control.gridRoles.getSelectedRows();
        if (data) {
            $.each(data, function (i, n) {
                roles.push(n.id);
            });
        }

        $.post('/API/Flow/SetFlowRole', {
            flow: row.Id,
            roles: liger.toJSON(roles)
        }, function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('分配成功', '提示');
                _window.control.dlgSetRole.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    _window.OnSetRoleShow = function (row) {
        if (!_window.control.gridRoles) {
            _window.control.gridRoles = $('#gridRoles').ligerGrid({
                columns: _window.options.roleColumns,
                isChecked: function (r) {
                    return r.checked;
                },
                resizable: false,
                frozen: false,
                usePager: false,
                checkbox: true
            });
        }

        _window.load(_window.control.gridRoles, '/API/Flow/GetFlowRole', row);
    }

    //_window = function () {
    //    DoActionConfirm(
    //        _window.control.grid,
    //        _window.OnDelete,
    //        _window.options.deleteQuestion,
    //        _window.options.deleteConfirm);
    //}

    _window.delete1 = function () {
        DoActionConfirm(_window.control.grid, _window.OnDelete, _window.options.deleteQuestion, _window.options.deleteConfirm);
    }

    $(document).ready(function () {
        _window.init();
        _window.load(_window.control.grid, '/API/Flow/Query', {});
    });
</script>